﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Seller
{
    public partial class wfDjackInfo : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //初始化用户信息
        private void getDjackInfo()
        {
            string djackId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djackId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataSet ds = vehicleInfo.SelectByIds(djackId, "");
                if (ds != null)
                {
                    string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 50, "");
                    this.ExecJScript(@"setDjackInfo(" + retJson + ")");
                }
            }
        }

        private void saveUserInfo()
        {
            string userId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userId))
            {
                this.ExecJScript(@"window.location.href = ""/""");
                return;
            }
            string userName = this.GetParamHtValue("userName");
            if (string.IsNullOrEmpty(userName))
            {
                this.ExecJScript(@"layer.alert('昵称不能为空',3)");
                return;
            }
            string sex = this.GetParamHtValue("sex");
            if (string.IsNullOrEmpty(sex))
            {
                this.ExecJScript(@"layer.alert('性别不能为空',3)");
                return;
            }
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destionId = this.GetParamHtValue("destionId");
            if (string.IsNullOrEmpty(destionId) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(countryId))
            {
                this.ExecJScript(@"layer.alert('地接客所在地不能为空',3)");
                return;
            }
            string qq = this.GetParamHtValue("qq");
            string mobile = this.GetParamHtValue("mobile");
            if (string.IsNullOrEmpty(mobile))
            {
                this.ExecJScript(@"layer.alert('手机不能为空',3)");
                return;
            }
            string phone = this.GetParamHtValue("phone");
            string micro = this.GetParamHtValue("micro");
            string email = this.GetParamHtValue("email");
            if (string.IsNullOrEmpty(email))
            {
                this.ExecJScript(@"layer.alert('邮箱不能为空',3)");
                return;
            }
            string djackImg = this.GetParamHtValue("djackImg");
            if (string.IsNullOrEmpty(djackImg))
            {
                this.ExecJScript(@"layer.alert('地接客头像不能为空',3)");
                return;
            }
            string briefIntrodu = this.GetParamHtValue("briefIntrodu");
            using (msBsc.AppLogic.DataAlc.CBizDjackSpecieInfo specieInfo = new AppLogic.DataAlc.CBizDjackSpecieInfo())
            {
                string[] retArry = specieInfo.EditDjack(userId, userName, djackImg, sex, briefIntrodu, destionId, cityId, countryId, mobile, phone, qq, micro, email);
                if (retArry[0] != "TRUE")
                {
                    this.Alert(retArry[1]);
                }
                else
                {
                    msBsc.SvRef.UserInfo.doSetAuthCookie(msBsc.SvRef.UserInfo.Operator, msBsc.SvRef.UserInfo.OperType, "", userName);
                    this.ExecJScript(@"layer.alert('修改成功',1);window.location.reload(true);");
                }
            }
        }

        #region 初始化地区下拉列表

        private void showPlace1()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string country = this.GetParamHtValue("country");
            string countryName = this.GetParamHtValue("countryName");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(country))
                {
                    country = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    countryName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('country_select',200,'1'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + country + "','" + countryName + "');");
                showPlace2(country);
            }

        }
        private StringBuilder[] packageCityArray(DataSet ds)
        {
            StringBuilder[] cityArray = new StringBuilder[2];
            StringBuilder listValue = new StringBuilder();
            StringBuilder listOption = new StringBuilder();
            listValue.Append(@"[");
            listOption.Append(@"[");
            bool isFirst = true;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    listValue.Append(",");
                    listOption.Append(",");
                }
                listValue.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"] + @"""");
                listOption.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"] + @"""");
            }
            listValue.Append(@"]");
            listOption.Append(@"]");
            cityArray[0] = listValue;
            cityArray[1] = listOption;
            return cityArray;
        }
        private void showPlace2(string strPlace1Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string city = this.GetParamHtValue("city");
                string cityName = this.GetParamHtValue("cityName");
                DataSet ds = cbcc.SelectCountryByParentId(strPlace1Id);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(city))
                {
                    city = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    cityName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('city_select',200,'2'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + city + "','" + cityName + "');");
                showPlace3(city);
            }
        }
        private void showPlace3(string strPlace2Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string destion = this.GetParamHtValue("destion");
                string destionName = this.GetParamHtValue("destionName");
                DataSet ds = cbcc.SelectCountryByParentId(strPlace2Id);
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(destion))
                {
                    destion = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    destionName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('destion_select',200,'3'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + destion + "','" + destionName + "');");
            }
        }
        #endregion

        #region 地区下拉列表改变
        private void getArea2()
        {
            string parentsId = this.GetParamHtValue("parentsId");

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"updateSelet('city_select'," + retJson + ");");
                getArea3(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }
        }
        private void getArea3(string getArea3Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string isAll = this.GetParamHtValue("isAll");
                DataSet ds = cbcc.SelectCountryByParentId(getArea3Id);
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"updateSelet('destion_select'," + retJson + ");");
            }
        }
        #endregion

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getDjackInfo();
            }
            else if (this.strAction == "initCity")
            {
                showPlace1();
            }
            else if (this.strAction == "getArea2")
            {
                getArea2();
            }
            else if (this.strAction == "getArea3")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea3(parentsId);
            }
            else if (this.strAction == "saveUserInfo")
            {
                saveUserInfo();
            }
        }
    }
}